package com.kfm.web;

import com.kfm.util.Constant;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/online")
public class OnlineServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取登录用户的数量 --> ServletContext 中的值
        ServletContext servletContext = req.getServletContext();
        List<String> list = (List<String>) servletContext.getAttribute(Constant.ONLINE_KEY);
        resp.setContentType("text/html;charset=utf-8");
        StringBuilder html = new StringBuilder("<h2>在线人数" + list.size() + "</h2>");
        for (String s : list) {
            html.append("<p>").append(s).append("</p>");
        }
        resp.getWriter().write(html.toString());

    }
}
